﻿using System;
using System.Collections;
using System.Collections.Generic;
using HTTP;
using HTML;
using Graph;

namespace Crawler
{
    public class Crawler
    {
        private Http http;
        private BasicGraph bGraph;
        List<string> links;

        public Crawler(string url)
        {
            links = new List<string>();
            bGraph = new BasicGraph(GetNumberOfLinks(url));
            Vertex root = new Vertex(url);
            bGraph.AddVertex(root);
            ConvertLinksToVertex(root);
           


            // test


        }

        private void ConvertLinksToVertex(Vertex root)
        {
            //Console.WriteLine(root.Name);
            http = new Http();
            string wString = http.OpenStream(root.Name);
            http.CloseStream();

            HTMLDoc doc = new HTMLDoc { _document = wString, rootUrl = root.Name };

            foreach (string s in doc.GetLinks())
            {
                Vertex v = bGraph.findVertex(s);
                if (v == null)
                {
                    v = new Vertex(s);
                    bGraph.AddVertex(v);
                    bGraph.AddEdge(root, v);
                    ConvertLinksToVertex(v);
                }
                else
                {
                    bGraph.AddEdge(root, v);
                }
            }
        }
        
        private int GetNumberOfLinks(string url)
        {
            int result = 1;

            http = new Http();
            string wString = http.OpenStream(url);
            http.CloseStream();

            HTMLDoc doc = new HTMLDoc { _document = wString, rootUrl = url };
            foreach (string s in doc.GetLinks())
            {
                if(!links.Contains(s))
                {
                    links.Add(s);
                    int nLinks = GetNumberOfLinks(s);
                    result += nLinks;
                }
            }
            return result;
        }

        public BasicGraph BGraph
        {
            get { return bGraph; }
        }
    }
}
